import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import { getToken } from '@/utils/auth'

// 创建mock适配器
function setupMock() {
  const mock = new MockAdapter(axios)

  // 登录接口
  mock.onPost('/vue-element-admin/user/login').reply(config => {
    const { username } = JSON.parse(config.data)
    if (username === 'admin' || username === 'editor') {
      return [
        200,
        {
          code: 20000,
          data: {
            token: username + '-token'
          }
        }
      ]
    }
    return [
      200,
      {
        code: 60204,
        message: '账号或密码不正确'
      }
    ]
  })

  // 获取用户信息接口
  mock.onGet(/\/vue-element-admin\/user\/info.*/).reply(config => {
    const token = getToken()
    let roles = ['editor']
    let name = 'Normal Editor'
    let avatar = 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'
    
    if (token === 'admin-token') {
      roles = ['admin']
      name = 'Super Admin'
    }
    
    return [
      200,
      {
        code: 20000,
        data: {
          roles,
          name,
          avatar,
          introduction: '我是管理员'
        }
      }
    ]
  })

  // 登出接口
  mock.onPost('/vue-element-admin/user/logout').reply(() => {
    return [
      200,
      {
        code: 20000,
        data: 'success'
      }
    ]
  })
}

export default setupMock 